// xc_lshop/shop/user/warehouse/com/choose.js
var actbill = require('../../../../../xc/model/user_warehouse')

Component({
  options: {
    addGlobalClass: true,
    multipleSlots: true
  },
  /**
   * 组件的属性列表
   */
  properties: {
    config:{
      type:Object,
      value:{}
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
   show:false,
   type:"recovery",//类型 recovery回收,
   select:{}
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onComfirm(){
      var select=this.data.select;
      var allnum=0;
      for(var i in select){
        console.log("allnumxxxx")
        console.log(select[i])
        if(select[i]['choosenum']){
          allnum +=select[i]['choosenum']

        }
      }
      console.log("allnum")
      console.log(allnum)

      if(allnum>0){
        var that=this;
        actbill.recovery(this.data.item.proid, select,this.data.type).then(function (result) {
           console.log("result")
           console.log(result)
           if(result['status']==1){
            wx.showToast({
              title: '交易成功',
            })
            that.triggerEvent("aftertransaction", {},  {})
            that.onClose();

           }else{
             wx.showModal({
               title: '错误',
               content:result['message'],
               complete: (res) => {
                 
               }
             })
           }

        })
      }else{
        wx.showToast({
          icon:"none",
          title: '请至少选择一个数量',
        })
      }
    },
    onChange(e){
       var val=e.detail;
       var itemname=e.currentTarget.dataset.itemname;
       console.log("e")
       console.log(e)

       var ident=itemname['pro_ident'];
       
       var attritem={}
       if(this.data.select[ident]){
         if(val==0){
          delete this.data.select[ident]
         }else{
          attritem=this.data.select[ident]
          attritem['choosenum'] = val
         }
        
       }else{
        attritem=JSON.parse(JSON.stringify(itemname));  
        attritem['choosenum']= val

       }
       console.log(attritem)
       this.setData({
          ['select.'+ident]:attritem
       })
       console.log(this.data.select)

    },
    init(item,type){
      this.setData({
        item:item,
        type:type,
        show:true
      })
    },
    onClose(){
      this.setData({
        show:false
      })
    }
  }
})
